package com.doit.demo01;

import java.io.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;

public class test05 {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\work\\abc\\IO练习题\\第四题\\于和伟.mp4");//视频路径
        File dir = new File("D:\\work\\abc\\IO练习题\\第四题\\part");//拆分路径
        if (!dir.exists())
            dir.mkdirs();

        ArrayList<File> list = new ArrayList<>();
        break_Up(file,dir,list);//拆分
        add_file(dir,list);//合成
        method();//信息
    }

    private static void add_file(File dir,ArrayList<File>list) throws IOException {

        FileOutputStream out = new FileOutputStream(dir.getPath() + "\\" + "于和伟.mp4");
        int i = 0;
        for (File name : list) {
            FileInputStream file = new FileInputStream(name);
            if (name.getName().toLowerCase().endsWith(".part"))//如果是文件且是part结尾
            {
                int len;
                byte[] bytes = new byte[1024*100];
                while ((len=file.read(bytes))!=-1){
                    out.write(bytes,0 ,len );
                    out.flush();
                }
                file.close();
                System.out.println("正在提取第"+(++i)+"个文件");
            }
        }


//        File[] dirr = dir.listFiles();
//        for (File file : dirr) {
//            if (file.isFile() && file.getName().toLowerCase().endsWith(".part"))//如果是文件且是part结尾
//            {
//                System.out.println(file.getName());
//            }
//        }


        System.out.println("合并完成");
        out.close();
    }

    public static void break_Up(File file, File dir ,ArrayList<File> list) throws IOException {
        int i = 0;
        FileInputStream in = new FileInputStream(file);//读取
        int len;
        byte[] bytes = new byte[1024*100];
        while ((len=in.read(bytes))!=-1){
//            System.out.println(len);
            String name = dir.getPath()+"\\"+(++i)+".part";//每个文件名称
            FileOutputStream out = new FileOutputStream(name);
            out.write(bytes,0 ,len );
            list.add(new File(name));

            out.flush();
            out.close();
        }
        in.close();
        System.out.println("已拆分好");
//        file.delete(); //删除源文件

        //信息存储
        Properties p = new Properties();
        p.setProperty("count",i+"");
        p.setProperty("name",file.getName());
        FileWriter fw = new FileWriter("D:\\work\\abc\\IO练习题\\第四题\\part\\person.properties");
        p.store(fw,"message"); //写入
        fw.close();
    }
    public static void method() throws IOException {
        Properties p = new Properties();
        Reader r = new FileReader("D:\\work\\abc\\IO练习题\\第四题\\part\\person.properties");
        p.load(r);
        r.close();
        System.out.println(p);
    }
}
